package com.sixnimmt.app.server.game.domain;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.TreeSet;

import com.sixnimmt.app.shared.game.domain.Card;
import com.sixnimmt.app.shared.game.domain.CardComparator;
import com.sixnimmt.app.shared.game.exception.NotInHandException;

public class Player implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final String name;

	private final String sessionId;

	private int score;

	private final TreeSet<Card> hand;

	private final LinkedHashSet<Card> takenCards;

	public Player(String name, String sessionId) {
		this.name = name;
		this.sessionId = sessionId;
		score = 0;
		hand = new TreeSet<Card>(new CardComparator());
		takenCards = new LinkedHashSet<Card>();
	}

	public void addCardInHand(Card card) {
		hand.add(card);
	}

	public void takeCards(LinkedHashSet<Card> cards) {
		takenCards.addAll(cards);
	}

	public PlayerChoice playCard(Card card) throws NotInHandException {
		if (!hand.contains(card))
			throw new NotInHandException();
		hand.remove(card);
		return new PlayerChoice(this, card);
	}

	public void clearCards() {
		hand.clear();
		takenCards.clear();
	}

	public void updateScore() {
		for (Card card : takenCards) {
			score += card.getBuffaloHeads();
		}
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public String getSessionId() {
		return sessionId;
	}

	public LinkedHashSet<Card> getTakenCards() {
		return takenCards;
	}

	public TreeSet<Card> getHand() {
		return hand;
	}

	@Override
	public String toString() {
		return name + ": " + score;
	}

	public String getHandToString() {
		return hand.toString();
	}

	public String getTakenCardsToString() {
		int points = 0;
		for (Card card : takenCards) {
			points += card.getBuffaloHeads();
		}
		return takenCards.size() + " card(s) : " + points + " point(s)";
	}
}
